from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from info import create_app, db, models


app = create_app("develope")
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command("xxxx", MigrateCommand)

# 使用脚本创建管理员账户
@manager.option('-n', '--name', dest='user_name')
@manager.option('-p', '--pwd', dest='user_pwd')
def create_super_user(user_name, user_pwd):
    if not all([user_name, user_pwd]):
        print("参数不足")
    # 创建管理员用户
    user = models.User()
    user.mobile = user_name
    user.nick_name = user_name
    user.password = user_pwd
    user.is_admin = True
    try:
        db.session.add(user)
        db.session.commit()
        print("创建管理员成功！")
    except Exception as e:
        print("创建用户失败!")
        db.session.rollback()




if __name__ == '__main__':
    manager.run()

